package com.concurrent.strings;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author: zhouyang
 * @descrpition: 线程不安全 使用joda-time或者堆栈封闭解决异常问题
 * @time: 2019-05-06 15:07
 */
@Slf4j
public class SimpleDateFormatTest {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    //请求总数
    public static int clientTotal = 5000;

    //请求并行的线程数
    public static int threadTotal = 200;

    public static void main(String[] args) throws Exception{
        Stack<Integer> stack = new Stack<>();
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);//请求总数
        for(int i=0;i<clientTotal;++i){
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    update();
                    semaphore.release();
                }catch (Exception e){

                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
    }

    private static void update(){
        try {
            //这样是不对的 会报异常
            Date date  = simpleDateFormat.parse("20180208");
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
